﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.Biz;
using Sector7.Medical.Consts;
using Sector7.Medical.Utility;
using Sector7.Medical.Controls;
using Sector7.Medical.Enums;
using Sector7.Framework.Core;
using Sector7.Medical.EntityType;
using Sector7.Framework.Enums;

namespace Sector7.Medical.STOM.Controls
{
    /// <summary>
    /// AddAdjustRecordControl.xaml 的交互逻辑
    /// </summary>
    public partial class AddAdjustRecordControl : AddModeControlBase
    {
        #region Accessor
        /// <summary>
        /// 获取或设置调价记录
        /// </summary>
        public M7_AdjustRecord Record
        {
            get { return m_entity as M7_AdjustRecord; }
            set { m_entity = value; }
        }
        #endregion

        #region Public functions
        #endregion

        #region Private functions
        protected override void InitializeForCreating()
        {
            stpMain.Children.Clear();
            stpMain.Children.Add(txbPrompt);

            dtpEffectDate.SelectedDate = DateTime.Now;
            var recordDate = ((DateTime)dtpEffectDate.SelectedDate).ToString("yyyyMMdd");
            var seed = Agent_SYSS.GetBinarySeed(C_M7_TableName.ADJUST_RECORD, C_BillType.ADJUST_RECORD.ToString());
            m_recordId = Convert.ToInt64(PrimaryKeyUtil.GetPrimaryKey(
                C_M7_TableName.ADJUST_RECORD,
                recordDate,
                C_BillType.ADJUST_RECORD,
                seed));
            txbRecordId.Text = m_recordId.ToString();
            txbOperator.Tag = Menvironments.CurrentAccount;
            txbOperator.Text = Menvironments.CurrentAccount.FullName;

            Record = new M7_AdjustRecord();
            Record.Items = new List<M7_AdjustRecordItem>();

            base.InitializeForCreating();
        }

        protected override void AddData()
        {
            Record.RecordId = m_recordId;
            Record.OperatorId = Menvironments.CurrentAccount.CitizenId;
            Record.DeptId = Menvironments.CurrentDepartment.DeptId;
            Record.CheckerId = Record.OperatorId;
            Record.EffectTime = (DateTime)dtpEffectDate.SelectedDate;
            Record.Remarks = txbRemarks.Text;

            foreach (DrugItemControlForADJ drugItemControl in stpMain.Children)
            {
                var item = drugItemControl.GetItem(m_recordId);
                if (item != null)
                {
                    Record.Items.Add(item);
                }
            }

            Agent_STOS.AddAdjustRecord(Record);

            ResetControl();

            base.AddData();
        }

        private void AddDrugItemControl()
        {
            var drugItemControl = new DrugItemControlForADJ();
            drugItemControl.SetPriceRange(m_rangeOfPurchases, m_rangeOfSales);
            drugItemControl.ClickAdd += new EventHandler(DrugItemControl_ClickOk);
            drugItemControl.ClickDelete += new EventHandler(DrugItemControl_ClickDelete);

            stpMain.Children.Add(drugItemControl);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (m_crud == CrudType.Unknown)
            {
                InitializeControl(CrudType.Create);
            }
        }

        private void txbPriceRange_LostFocus(object sender, RoutedEventArgs e)
        {
            var txbPriceRange = sender as TextBox;
            int flag = Convert.ToInt32(txbPriceRange.Tag);

            try
            {
                decimal priceRange = Convert.ToDecimal(txbPriceRange.Text);

                switch (flag)
                {
                    case 1: // PurchasesPrice
                        m_rangeOfPurchases = priceRange;
                        break;
                    case 2: // SalesPrice
                        m_rangeOfSales = priceRange;
                        break;
                    default:
                        break;
                }

                if (stpMain.Children[0] is DrugItemControlForADJ)
                {
                    foreach (DrugItemControlForADJ drugItemControl in stpMain.Children)
                    {
                        drugItemControl.SetPriceRange(m_rangeOfPurchases, m_rangeOfSales);
                    }
                }
            }
            catch
            {
                Alert.ShowDialog(AlertType.Error, "请输入正确的数字设置价格涨幅！");
            }
        }

        private void DrugItemControl_ClickOk(object sender, EventArgs e)
        {
            AddDrugItemControl();
        }

        private void DrugItemControl_ClickDelete(object sender, EventArgs e)
        {
            stpMain.Children.Remove(sender as DrugItemControlForADJ);

            if (stpMain.Children.Count == 0)
            {
                stpMain.Children.Add(txbPrompt);
            }
        }

        private void lblStart_MouseDown(object sender, MouseButtonEventArgs e)
        {
            stpMain.Children.Clear();

            AddDrugItemControl();
        }

        private void lblStart_MouseEnter(object sender, MouseEventArgs e)
        {
            lblStart.Cursor = Cursors.Hand;
        }

        private void lblStart_MouseLeave(object sender, MouseEventArgs e)
        {
            lblStart.Cursor = Cursors.Arrow;
        }
        #endregion

        #region Structure
        public AddAdjustRecordControl()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        private long m_recordId;
        private decimal m_rangeOfPurchases = 0;
        private decimal m_rangeOfSales = 0;
        #endregion
    }
}
